// pages/addRemind/addRemind.js
import { shareOptions } from '../../utils/access';
import request from '../../utils/request'
const api = require("../../config/api");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    titleList:[
      '提醒：该去扔垃圾了！',
      '提醒：厨余垃圾要正确投放！'
    ],
    midTextList:[
      '设置一个时间，小K会提醒您及时清理、投放生活垃圾',
      '设置一个您用餐的时间，小K会提醒您及时处理厨余垃圾'
    ],
    id: null,
    // 选中时间
    time: '',
    // 时间列表
    timeList: [
      '00:00',
      '01:00',
      '02:00',
      '03:00',
      '04:00',
      '05:00',
      '06:00',
      '07:00',
      '08:00',
      '09:00',
      '10:00',
      '11:00',
      '12:00',
      '13:00',
      '14:00',
      '15:00',
      '16:00',
      '17:00',
      '18:00',
      '19:00',
      '20:00',
      '21:00',
      '22:00',
      '23:00',
    ],
    // 星期列表
    list: [
      { value: 1, text: '周一', select: true, },
      { value: 2, text: '周二', select: false, },
      { value: 3, text: '周三', select: false, },
      { value: 4, text: '周四', select: false, },
      { value: 5, text: '周五', select: false, },
      { value: 6, text: '周六', select: false, },
      { value: 7, text: '周日', select: false, },
    ],
    modalButtons: [
      { text: "取消" },
      { text: "提交" },
    ],
    modal: {
      show: false,
      title: "提醒",
      content: "确定要删除这个提醒吗？",
    },
    type:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id,
        time: options.time,
        list: this.data.list.map(item => {
          return {
            ...item,
            select: (options.weeks?.split(',') || []).includes(item.value.toString())
          }
        })
      })
    }

    if(options.type){
      this.setData({
        type:Number(options.type)
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.deleteModal = this.selectComponent('#deleteModal')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // 周点击
  clickItem: function (e) {
    const data = e.currentTarget.dataset
    this.setData({
      list: this.data.list.map(item => {
        if (item.text === data.item?.text) {
          item.select = !item.select
        }
        return item
      })
    })
  },
  // 选择时间
  bindTimeChange: function (e) {
    this.setData({
      time: this.data.timeList[e.detail.value]
    })
  },
  // 提交
  async submit(deleted) {
    if (this.submiting) return
    
    const { list, time, id, type } = this.data
    if(!time){
      wx.showToast({
        icon: 'none',
        title: '请选择提醒时间！',
      })
      return
    }
    this.submiting = true
    const weeks = list.filter(l => l.select)
    if (weeks.length) {
      try {
        const res = await request.post(id ? api.warningUpdate : api.warningAdd, {
          remindDate: weeks.map(m => m.value).toString(),
          remindTime: time,
          ...(id ? { id } : {}),
          ...(deleted === 1 ? { deleted } : {}),
          remindType:type,
        })
        console.log(res);
        wx.navigateBack({
          delta: id ? 1 : 2,
        })
      } catch { }
    } else {
      wx.showToast({
        icon: 'none',
        title: '请选择每周几提醒！',
      })
    }
    this.submiting = false
  },
  // 显示弹窗
  showModal() {
    const { modal } = this.data
    this.setData({
      modal: {
        ...modal,
        show: true
      }
    })
    this.deleteModal.show()
  },
  // 隐藏弹窗
  hideModal() {
    const { modal } = this.data
    this.setData({
      modal: {
        ...modal,
        show: false
      }
    })
    this.deleteModal.hide()
  },
  // 删除
  async remove(e) {
    console.log(e);
    // if (e.detail.index) {
    await this.submit(1)
    // }
    this.hideModal()
  },
   // 点击弹窗--取消
   cancle(){
    this.deleteModal.hide()
  },
  // 用户点击右上角分享
	onShareAppMessage(e) {
		return shareOptions('default',e)
	},
})